import CommonTransfer from '@/component/CommonTransfer';
import { addCoupon, updateCouponRuleApi } from '@/services/api';
import {
  DrawerForm,
  ProFormDateTimePicker,
  ProFormDigit,
  ProFormInstance,
  ProFormText,
  ProFormItem
} from '@ant-design/pro-components';
import { Button, Form, Space, Tag, message } from 'antd';
import dayjs from 'dayjs';
import React, { useEffect, useRef, useState } from 'react';

interface Props {
  open?: boolean;
  info?: any;
  close?: () => void;
  onFinish?: () => void;
}

const CreateCouponDrawer: React.FC<Props> = ({
  open,
  close,
  info,
  onFinish,
}) => {
  const [form] = Form.useForm();

  const formRef = useRef<ProFormInstance>();

  const usersValue = Form.useWatch('users', form);

  // 是否打开配置商户弹窗
  const [openMerchant, setOpenMerchant] = useState(false);

  useEffect(() => {
    console.log('infoinfoinfo',info);
    
  },[info])

  return (
    <DrawerForm
      formRef={formRef}
      title={info.id ? '编辑优惠券规则' : '新建优惠券规则'}
      form={form}
      open={open}
      width={500}
      onOpenChange={(visible) => {
        if (!visible) {
          close?.();
        }
      }}
      autoFocusFirstInput
      drawerProps={{
        destroyOnClose: true,
      }}
      initialValues={{ ...info }}
      onFinish={async (values) => {
        try {
          if (!info?.id) {
            const { msg } = await addCoupon({
              ...values,
            });

            message.success(msg);
          } else {
            const { msg } = await updateCouponRuleApi(info?.id, {
              ...values,
            });

            message.success(msg);
          }

          onFinish?.();
          // 不返回不会关闭弹框
          return true;
        } catch (error) {}
      }}
    >
      <ProFormText
        width="lg"
        name="code"
        label="券码"
        placeholder="请输入券码"
        rules={[
          {
            required: true,
          },
        ]}
      />

      <ProFormText
        width="lg"
        name="title"
        label="优惠券规则名称"
        placeholder="请输入优惠券规则名称"
        rules={[
          {
            required: true,
          },
        ]}
      />

      <ProFormDigit
        width="lg"
        name="frequency"
        label="限制次数"
        placeholder="请输入限制次数"
        rules={[
          {
            required: true,
          },
        ]}
        fieldProps={{
          type: 'number',
          min: 1,
        }}
      />

      <ProFormItem
        name="users"
        label="商户关联"
        rules={[
          {
            required: true,
          },
        ]}
      >
        <Space>
          <Button
            onClick={() => {
              setOpenMerchant(true);
            }}
          >
            配置商户
          </Button>

          {usersValue?.length > 0 && (
            <Tag> 已选商户数量：{usersValue?.length}</Tag>
          )}
        </Space>
      </ProFormItem>

      <CommonTransfer
        open={openMerchant}
        onClose={() => {
          setOpenMerchant(false);
        }}
        onOk={(vales) => {
          form.setFieldValue('users', vales);
          setOpenMerchant(false);
        }}
        initValues={usersValue}
      />

      {/* <ProFormDateTimePicker
        width="lg"
        name="date"
        label="限制时间"
        fieldProps={{
          disabledDate: (current) => {
            // Can not select days before today and today
            return current && current < dayjs().endOf('day');
          },
        }}
        rules={[
          {
            required: true,
          },
        ]}
      /> */}
    </DrawerForm>
  );
};

export default CreateCouponDrawer;
